package cao.oschina.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cao.oschina.R;
import hd.util.ScreenHelper;

/**
 * Table的ItemView，包含图像和标题
 * <p/>
 * Created by Haidy on 15/3/6.
 */
public class TabItemView extends LinearLayout {

    private ImageView imageView;
    private Drawable unCheckDrawable;
    private Drawable checkDrawable;
    private int checkedTitleColor = Color.BLUE;
    private int unCheckTitleColor = Color.BLACK;
    private String title;

    private TextView textView;

    private boolean checked = false;

    public TabItemView(Context context) {
        this(context, null);
    }

    public TabItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TabView);
        if (typedArray != null) {
            int count = typedArray.getIndexCount();
            int index;
            for (int i = 0; i < count; i++) {
                index = typedArray.getIndex(i);
                if (index == R.styleable.TabView_checkedDrawable) {
                    checkDrawable = typedArray.getDrawable(index);
                } else if (index == R.styleable.TabView_unCheckDrawable) {
                    unCheckDrawable = typedArray.getDrawable(index);
                } else if (index == R.styleable.TabView_checkedTitleColor) {
                    checkedTitleColor = typedArray.getColor(index, Color.BLUE);
                } else if (index == R.styleable.TabView_unCheckTitleColor) {
                    unCheckTitleColor = typedArray.getColor(index, Color.BLACK);
                } else if (index == R.styleable.TabView_itemChecked) {
                    checked = typedArray.getBoolean(index, false);
                } else if (index == R.styleable.TabView_title) {
                    title = typedArray.getString(index);
                }
            }
            typedArray.recycle();
        }
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        setOnClickListener(listener);
        initView();
        initData();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        imageView = new ImageView(getContext());
        imageView.setLayoutParams(new LayoutParams(ScreenHelper.dp2px(getContext(), 30), ScreenHelper.dp2px(getContext(), 30)));
        addView(imageView);
        textView = new TextView(getContext());
        LayoutParams textLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textLp.topMargin = ScreenHelper.dp2px(getContext(), 0); //距离
        textView.setLayoutParams(textLp);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        textView.setGravity(Gravity.CENTER);
        addView(textView);
    }

    public void check(boolean checked) {
        this.checked = checked;
        if (checked) {
            imageView.setImageDrawable(checkDrawable);
            textView.setTextColor(checkedTitleColor);
        } else {
            imageView.setImageDrawable(unCheckDrawable);
            textView.setTextColor(unCheckTitleColor);
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {
        check(checked);
        if (title != null) {
            textView.setText(title);
        }
    }

    private OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!checked) {
                checked = true;
            }
        }
    };
}
